//
//  QNCircleProgress.h
//  QNCircleProgress
//
//  Created by Kalan on 2019/12/6.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, QNCircleType) {
    QNCircleTypeGradientView,
    QNCircleTypeGradientLayer
};

@interface QNCircleConfig : NSObject

/// 圆环类型
@property (assign, nonatomic) QNCircleType circleType;
/// 默认轨道背景色
@property (strong, nonatomic) UIColor *trackTintColor;
/// 颜色渐变数组，目前最多支持4种颜色渐变，如需更多需要扩展实现逻辑
@property (strong, nonatomic) NSArray <UIColor *> *colors;
/// 线宽
@property (assign, nonatomic) CGFloat lineWidth;
/// 动画时间，更新动画的频率，和定时器执行频率设为一致最佳
@property (nonatomic, assign) CGFloat animationDuration;

@end

@interface QNCircleProgress : UIView

/// 中间标签
@property (strong, nonatomic) UILabel *centerLabel;
/// 当前进度
@property (assign, nonatomic) CGFloat progress;
/// 当前颜色集合，只对QNCircleTypeGradientView生效
@property (strong, nonatomic, readonly) NSArray *colors;
// 动画执行完回调
@property (nonatomic, copy) void (^animatedCompletion)(CGFloat progress, BOOL finish);

/// 渐变进度条初始化方法
///
/// 使用示例
///
///        QNCircleConfig *config = QNCircleConfig.alloc.init;
///        config.trackTintColor = [UIColor colorWithRed:254/255.0 green:247/255.0 blue:204/255.0 alpha:1];
///        config.startColor = [UIColor colorWithRed:251/255.0 green:212/255.0 blue:0 alpha:1];
///        config.endColor = [UIColor colorWithRed:246/255.0 green:86/255.0 blue:4/255.0 alpha:1];
///        config.lineWidth = 40;
///        config.animationDuration = 1;
///        config.circleType = QNCircleTypeGradientView;
///        self.progressView = [QNCircleProgress.alloc initWithFrame:CGRectZero config:config];
///        [self.view addSubview:self.progressView];
///        [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
///            make.width.height.mas_equalTo(300);
///            make.center.mas_equalTo(0);
///        }];
///
///        self.progressView.progress = 1;
///        self.progressView.animationProgressCallBack = ^(CGFloat animationProgress) {
///            NSLog(@"%f", animationProgress);
///        };
///
/// @param frame 尺寸位置
/// @param config 配置实例
///
/// @return 进度条实例
- (instancetype)initWithFrame:(CGRect)frame config:(QNCircleConfig *)config;
/// 设置进度
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated;

// 翻转渐变颜色，只对QNCircleTypeGradientView生效
- (void)reverseGradienColor;
// 更新填充颜色，只对QNCircleTypeGradientView生效
- (void)updateCircleWithColors:(NSArray <UIColor *> *)colors animated:(BOOL)animated;

@end

NS_ASSUME_NONNULL_END


/*
 
 用法介绍：
 
 - (void)viewDidLoad
 {
     [super viewDidLoad];
 
     //第一种圆环
     QNCircleConfig *config1 = QNCircleConfig.alloc.init;
     config1.trackTintColor = [UIColor colorWithRed:254/255.0 green:247/255.0 blue:204/255.0 alpha:1];
     config1.colors = @[[UIColor kl_colorWithHexNumber:0x2ACAF7], [UIColor kl_colorWithHexNumber:0xEAE115], [UIColor kl_colorWithHexNumber:0xFF5500]];
     config1.lineWidth = 30;
     config1.animationDuration = 1;
     config1.circleType = QNCircleTypeGradientView;
     self.progressView1 = [QNCircleProgress.alloc initWithFrame:CGRectZero config:config1];
     self.progressView1.centerLabel.text = @"0℃";
     [self.view addSubview:self.progressView1];
     [self.progressView1 mas_makeConstraints:^(MASConstraintMaker *make) {
     make.width.height.mas_equalTo(self.view.mas_height).multipliedBy(1/3.5);
     make.centerX.mas_equalTo(0);
     make.top.mas_equalTo(20);
     }];
 
     __weak typeof(self) ws = self;
     __block CGFloat index = 0;
     [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
     index += (1/10.0);
     if (index >= 1) {
     [timer invalidate];
     return;
     }
     [ws.progressView1 setProgress:index animated:YES];
     ws.progressView1.centerLabel.text = [NSString stringWithFormat:@"%.0f℃", index * 100];
     }];
 
     self.progressView1.animatedCompletion = ^(CGFloat progress, BOOL finish) {
     NSLog(@"progressView1 finish %f %d", progress, finish);
     };
 
 
     //第二种圆环
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     QNCircleConfig *config2 = QNCircleConfig.alloc.init;
     config2.trackTintColor = [UIColor colorWithRed:254/255.0 green:247/255.0 blue:204/255.0 alpha:1];
     config2.colors = @[[UIColor kl_colorWithHexNumber:0xEAE115], [UIColor kl_colorWithHexNumber:0xFF5500]];
     config2.lineWidth = 30;
     config2.animationDuration = 1;
     config2.circleType = QNCircleTypeGradientView;
     self.progressView2 = [QNCircleProgress.alloc initWithFrame:CGRectZero config:config2];
     self.progressView2.centerLabel.text = @"0℃";
     [self.view addSubview:self.progressView2];
     [self.progressView2 mas_makeConstraints:^(MASConstraintMaker *make) {
     make.width.height.mas_equalTo(self.view.mas_height).multipliedBy(1/3.5);
     make.center.mas_equalTo(0);
     }];
 
     __block CGFloat index2 = 0;
     [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
     index2 += (1/10.0);
     if (index2 >= 1) {
     //                [timer invalidate];
     //                return;
     index2 = 0;
     [ws.progressView2 updateCircleWithColors:@[[UIColor kl_colorWithHexNumber:0x2ACAF7], [UIColor kl_colorWithHexNumber:0xEAE115], [UIColor kl_colorWithHexNumber:0xFF5500]] animated:YES];
     }
     [ws.progressView2 setProgress:index2 animated:YES];
     ws.progressView2.centerLabel.text = [NSString stringWithFormat:@"%.0f℃", index2 * 100];
     }];
 
     self.progressView2.animatedCompletion = ^(CGFloat progress, BOOL finish) {
     NSLog(@"progressView2 finish %f %d", progress, finish);
     };
     });
 
 
     //第三种圆环
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     QNCircleConfig *config3 = QNCircleConfig.alloc.init;
     config3.trackTintColor = [UIColor colorWithRed:254/255.0 green:247/255.0 blue:204/255.0 alpha:1];
     config3.lineWidth = 30;
     config3.animationDuration = 1;
     config3.circleType = QNCircleTypeGradientLayer;
     config3.colors = @[[UIColor kl_colorWithHexNumber:0xC10D00],
     [UIColor kl_colorWithHexNumber:0xFF6A00],
     [UIColor kl_colorWithHexNumber:0xC20E02]];
     self.progressView3 = [QNCircleProgress.alloc initWithFrame:CGRectZero config:config3];
     self.progressView3.centerLabel.text = @"0℃";
     [self.view addSubview:self.progressView3];
     [self.progressView3 mas_makeConstraints:^(MASConstraintMaker *make) {
     make.width.height.mas_equalTo(self.view.mas_height).multipliedBy(1/3.5);
     make.centerX.mas_equalTo(0);
     make.bottom.mas_equalTo(-20);
     }];
     //            self.progressView3.progress = 0.6;
 
     __block CGFloat index3 = 0;
     [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
     index3 += (1/10.0);
     if (index3 >= 1) {
     [timer invalidate];
     return;
     }
     [ws.progressView3 setProgress:index3 animated:YES];
     ws.progressView3.centerLabel.text = [NSString stringWithFormat:@"%.0f℃", index3 * 100];
     }];
 
     self.progressView3.animatedCompletion = ^(CGFloat progress, BOOL finish) {
     NSLog(@"progressView3 finish %f %d", progress, finish);
     };
     });
 }
 
 
 
 */


